var StateTwo = {

  preload: () => {
    game.load.spritesheet("birdFly", 'img/birds.png', 48, 48)
    game.load.image('pipe', "img/pipe.png")
    game.load.audio("music", "music/bgmusic.mp3")
    game.load.image("pauseBtn",'img/pause.png')
    game.load.image("backgroundNight",'img/backgroundNight.png')
  },

  create: () => {
    game.score = 0
    game.timeId = 0
    game.level = 0
    game.pipe_timeCeil = 300
    game.pipe_speed = 100
    game.pipe_ceil = 10

    background = game.add.tileSprite(0, 0, game.width, game.height, 'background')
    background.scale.setTo(2, 2)

    bird = game.add.sprite(game.world.centerX - 50, game.world.centerY, 'birdFly')
    bird.anchor = new Phaser.Point(.5, .5)
    bird.animations.add("birdFly")
    bird.animations.play("birdFly", 10, true)

    ground = game.add.tileSprite(0, 920, game.world.width, 122, 'ground')

    text = game.add.text(game.world.centerX - 200, 50, "score: " + game.score, { font: "100 30px 'myfont'", fill: "#FFFFFF" });
    text.anchor = new Phaser.Point(.5, .5)
    text2 = game.add.text(game.world.centerX , 980, "level: " + game.level, { font: "100 30px 'myfont'", fill: "#FFFFFF" });
    text2.anchor = new Phaser.Point(.5, .5)
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.physics.arcade.enable(bird)

    bird.body.gravity.y = 1000


    game.time.events.loop(2000 - game.level * game.pipe_timeCeil, genPipeRow, this)

    pipes = game.add.group();

    sound = game.add.audio('music', 0.1);
    sound.play();

    pauseBtn = game.add.sprite(game.world.centerX + 250, 45, 'pauseBtn')
    pauseBtn.anchor = new Phaser.Point(.5, .5)
    pauseBtn.scale.setTo(2, 2)
    P = game.input.keyboard.addKey(Phaser.Keyboard.P);



  },

  update: () => {
    
    game.onPause.add(function () {
      clearInterval(game.timeId)
      game.timeId = setInterval(() => {
        if (game.input.activePointer.justPressed()) {
          game.paused = false
          bird.body.velocity.y = -260
          bird.angle = -15
        }
      }, 200);
    })

    ground.tilePosition.x -= 2;
    if (bird.angle < 90) {
      bird.angle += 2
    }

    if (P.isDown) {
      game.paused = true
    }



    if (game.input.activePointer.justPressed()) {
      game.paused = false
      bird.body.velocity.y = -260
      bird.angle = -15
    }

    text.bringToTop()
    ground.bringToTop();
    text2.bringToTop()

    if (bird.y > 920) {
      game.state.start("state_three")
      sound.stop();
    }


    game.physics.arcade.collide(bird, pipes, birdCollide, null, this)

    

  }
}


function genPipeRow() {
  height = Math.floor(Math.random() * 350)
  this.genPipe(game.world.width, height, true)
  this.genPipe(game.world.width, height + 200 -game.level * game.pipe_ceil, false)

  if (pipe.body.velocity.x < 288) {

    text.x = game.world.centerX - 200 + (numDigit(game.score) * 8)
    text.setText("score: " + game.score);
    changeLevel()
    if (game.level == 0){
      let i = 1
      game.score +=  1
    }else{
      game.score += 10 * game.level;
    }
    
  }
}


function genPipe(x, y, isInverted) {

  pipe = game.add.sprite(x, y, "pipe")
  pipe.scale.setTo(1,2)
  game.physics.arcade.enable(pipe)
  pipe.body.velocity.x = -150 ;


  pipe.anchor = new Phaser.Point(0.5, 0)

  if (isInverted) {
    pipe.scale.y = -1
  }

  pipes.add(pipe);
  pipe.checkWorldBounds = true;
  pipe.outOfBoundsKill = true;

}

function birdCollide(obj1, obj2) {
  sound.stop();
  game.state.start("state_three")
}

function numDigit(num) {
  let count = 0
  if (num >= 0 && num <= 999999999) {
    while (num != 0) {
      num = Math.floor(num / 10);
      count++;
    }
  }
  if (count == 1) {
    count = 0
  }
  return count
}

function sleep(number) {

  var now = new Date();
  var exitTime = now.getTime() + number;
  while (true) {
    now = new Date();
    if (now.getTime() > exitTime) {
      return;
    }
  }
}

function changeLevel(){
  if ( game.score >= 10 && game.score < 100){
    background.kill()
    background2 = game.add.tileSprite(0, 0, game.width, game.height, 'backgroundNight')
    background2.scale.setTo(2, 2)
    background2.sendToBack()
    game.level = 1
    text2.setText("level: " + game.level);
  }else if ( game.score >= 100 && game.score < 1000){
    background2.kill()
    background.reset()
    game.level = 2
    text2.setText("level: " + game.level);
  }else if ( game.score >= 100 && game.score < 10000){
    background.kill()
    background2.reset()
    game.level = 3
    text2.setText("level: " + game.level);
  }else if ( game.score >= 10000 ){
    background2.kill()
    background.reset()
    game.level = 4
    text2.setText("level: " + game.level);
  }
}